<template>
    <div class="yuan-container container">
       <a-card title="半成品库存表">
            <div style="text-align: right;">
                <a-button type="primary" @click="exportData">导出</a-button>
            </div>
           <a-tabs type="card" @change="tabChange">
               <a-tab-pane v-for="(item) in tabsData" :key="item" :tab="item">
                   <div class="filter-panel">
                    <span style="margin-right: 5px;">关键词搜索：</span>
                       <a-input
                           allowClear
                           v-model="searchKeyword"
                           placeholder="请输入关键词"
                           style="width: 200px; margin-right: 30px;"
                           @pressEnter="onSearch"
                       />
                       <span style="margin-right: 5px;">选择标准类型：</span>
                       <a-select
                           v-model="searchStandard"
                           placeholder="标准类型"
                           style="width: 120px; margin-right: 10px;"
                           @change="onSearch"
                           allow-clear
                       >
                            <a-select-option value="国标">国标</a-select-option>
                            <a-select-option value="省标">省标</a-select-option>
                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                       </a-select>
                       <span style="margin-right: 5px;">选择入库日期：</span>
                       <a-range-picker
                           v-model="searchStorageDate"
                           valueFormat="YYYY-MM-DD"
                           style="margin-right: 15px;"
                           @change="onSearch"
                           :locale="locale"
                       />
                       <span style="margin-right: 5px;">选择报告日期：</span>
                       <a-range-picker
                           v-model="searchReportDate"
                           valueFormat="YYYY-MM-DD"
                           style="margin-right: 15px;"
                           @change="onSearch"
                           :locale="locale"
                       />
                       <a-button  type="primary" @click="onSearch">搜索</a-button>
                       <div class="filter-list" style="float: right;margin-right: 0;margin-bottom: 0;">
                           <a-button  v-if="curTab ==='饮片库存表'|| curTab ==='收粉库存表' ||curTab ==='总混二库存表'" style="margin-right:5px" type="primary" @click="add">新增</a-button>
                           <a-button type="primary" style="margin-right:5px" @click="exportDataWithType">导出当前库存表</a-button>
                       </div>
                   </div>
                   <div style="display: flex;flex-direction: row-reverse;padding:0px 5px 5px 0">
                    <a-button style="margin-left: 5px;width: 130px;" type="primary" @click="downloadTemplate">下载导入模板</a-button>
                           <a-upload name="file" :multiple="false" :before-upload="beforeUpload" :showUploadList="false">
                               <a-button type="primary" style="margin-left: 5px;margin-right: 0">导入</a-button>
                           </a-upload>
                   </div>
                  <a-table
                      :scroll="{ x: 1500 }"
                      :loading="tableOption.loading"
                      bordered
                      :rowKey="item=>item.id"
                      :data-source="tableOption.dataSource"
                      :columns="tableOption.tableColumns"
                      :pagination="tableOption.pagination">
                  </a-table>
               </a-tab-pane>
           </a-tabs>
       </a-card>
        <!-- 弹窗表单 -->
        <addOrEdit :viewDetail="viewDetail" :formData="formData" :tabName="curTab" :visible="formShow" :editId="formEditId" @closeModal="formClose"></addOrEdit>
        <!-- 质量检验弹窗 -->
        <quality-check :tabName="curTab" :formData="formData" :visible="editQualityShow" :editId="formEditId" @closeModal="qualityCheckClose"></quality-check>
        <adjust-stock :formData="formData" :visible="adjustStockShow" @close="adjustStockClose"></adjust-stock>
    </div>
</template>
<script lang="ts">
import {Vue, Component, Watch} from 'vue-property-decorator';
import nextApi from "@/api/nextApi";
import {h} from "vue";
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";
import {UserModule} from "@/store/modules/userModules";
import {downLoadFile} from "@/utils";
import addOrEdit from '@/views/reportMgt/halfRawProduct/components/addOrEdit.vue'
import qualityCheck from '@/views/reportMgt/halfRawProduct/components/qualityCheck.vue'
import adjustStock from '@/views/reportMgt/halfRawProduct/components/adjustStock.vue'
@Component({
    components: {addOrEdit, qualityCheck, adjustStock}
})
export default class extends Vue {
    locale = locale;
    searchKeyword: string = '';
    searchStandard: string = '';
    searchStorageDate: string[] = [];
    searchReportDate: string[] = [];
    tabsData:string[] = []

    filteredTabsData(){
        if(this.$utilCheckPermission(['yp'])) {
            this.tabsData.push("饮片库存表");
        }
        if(this.$utilCheckPermission(['sf'])) {
            this.tabsData.push("收粉库存表");
        }
        if(this.$utilCheckPermission(['gs'])) {
            this.tabsData.push("过筛库存表");
        }
        if(this.$utilCheckPermission(['zhy'])) {
            this.tabsData.push("总混一库存表");
        }
        if(this.$utilCheckPermission(['zhe'])) {
            this.tabsData.push("总混二库存表");
        }
        if(this.$utilCheckPermission(['zl'])) {
            this.tabsData.push("制粒库存表");
        }
        this.tabChange(this.tabsData[0])
    }

    //表格配置
    tableOption: any = {
        loading:false,
        tableColumns: [],
        dataSource: [],
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0,
            showTotal: (total: number) => `共${total}条`,
            onChange: this.pageChange,
        }
    }
    mounted() {
        this.filteredTabsData()
    }
    //监听toggleCollapsed
    @Watch("state")
    stateChange(n){
        if(n){
            this.tableOption.width = 1760
        }else{
            this.tableOption.width = 1640
        }
    }
    //获取vuex中的toggleCollapsed
    get state(){
        return UserModule.toggleCollapsed
    }
    async getTableData(){
        try {
            this.tableOption.loading = true
            let params = {
                pageSize: this.tableOption.pagination.pageSize,
                page: this.tableOption.pagination.current,
                type: this.correctName(),
                type2:this.curTab.split('库存表')[0],
                name: this.searchKeyword,
                standard: this.searchStandard,
                inTimeStart: this.searchStorageDate && this.searchStorageDate[0] ? this.searchStorageDate[0] : undefined,
                inTimeEnd: this.searchStorageDate && this.searchStorageDate[1] ? this.searchStorageDate[1] : undefined,
                reportTimeStart: this.searchReportDate && this.searchReportDate[0] ? this.searchReportDate[0] : undefined,
                reportTimeEnd: this.searchReportDate && this.searchReportDate[1] ? this.searchReportDate[1] : undefined,
            }
            const res = await nextApi.getHalfRawPorduct(params)
            this.tableOption.dataSource = res.data?.records || []
            this.tableOption.pagination.total = res.data?.total || 0
        }finally {
            this.tableOption.loading = false
        }

    }
    pageChange(page: number, pageSize: number) {
        this.tableOption.pagination.current = page
        this.getTableData()
    }
    /**
     * @description tab切换
     * **/
    curTab: string = ''
    tabChange(e){
        this.curTab = e
        this.changeColumn()
    }
    correctName(){
        if(this.curTab === '饮片库存表'){
            return "饮片"
        }else if(this.curTab === '收粉库存表' || this.curTab === '过筛库存表' || this.curTab === '总混一库存表'){
            return "提取物"
        }else{
            return "制粒"
        }
    }
    changeColumn(){
        switch (this.curTab) {
            case '饮片库存表':
                this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "ypName",
                        key: "ypName",
                        align: "center",
                    },
                    {
                        title: "饮片批号",
                        dataIndex: "ypBatch",
                        key: "ypBatch",
                        align: "center",
                    },
                    {
                        title: "入库日期",
                        dataIndex: "ypInTime",
                        key: "ypInTime",
                        align: "center",
                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "ypInWeight",
                        key: "ypInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "ypBalanceWeight",
                        key: "ypBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "出库日期",
                        dataIndex: "ypOutTime",
                        key: "ypOutTime",
                        align: "center",
                    },
                    {
                        title: "出库重量(kg)",
                        dataIndex: "ypOutWeight",
                        key: "ypOutWeight",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "ypQualityState",
                        key: "ypQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "ypAmount",
                        key: "ypAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "ypReportTime",
                        key: "ypReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "ypValidity",
                        key: "ypValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "ypRecheckTime",
                        key: "ypRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "ypEdit",
                        key: "ypEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "ypDepartment",
                        key: "ypDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "ypRemark",
                        key: "ypRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                "详情"
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.ypBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ]
                this.getTableData()
                break;
            case "收粉库存表":
                this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "tqwName",
                        key: "tqwName",
                        align: "center",
                    },
                    {
                        title: "提取物批号",
                        dataIndex: "tqwBatch",
                        key: "tqwBatch",
                        align: "center",
                    },
                    {
                        title: "入库日期",
                        dataIndex: "sfInTime",
                        key: "sfInTime",
                        align: "center",
                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "sfInWeight",
                        key: "sfInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "sfBalanceWeight",
                        key: "sfBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "记录重量",
                        dataIndex: "recordWeight",
                        key: "recordWeight",
                        align: "center",
                    },
                    {
                        title: "结存记录重量",
                        dataIndex: "storageRecordWeight",
                        key: "storageRecordWeight",
                        align: "center",
                    },
                    {
                        title: "出库日期",
                        dataIndex: "sfOutTime",
                        key: "sfOutTime",
                        align: "center",
                    },
                    {
                        title: "出库重量(kg)",
                        dataIndex: "sfOutWeight",
                        key: "sfOutWeight",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "sfQualityState",
                        key: "sfQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "sfAmount",
                        key: "sfAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "sfReportTime",
                        key: "sfReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "sfValidity",
                        key: "sfValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "sfRecheckTime",
                        key: "sfRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "sfEdit",
                        key: "sfEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "sfDepartment",
                        key: "sfDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "sfRemark",
                        key: "sfRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                record.sfBalanceWeight>=0 ? '详情':'编辑'
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.sfBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ]
                this.getTableData()
                break;
            case "过筛库存表":
            this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "tqwName",
                        key: "tqwName",
                        align: "center",
                    },
                    {
                        title: "提取物批号",
                        dataIndex: "tqwBatch",
                        key: "tqwBatch",
                        align: "center",
                    },
                    {
                        title: "件数",
                        dataIndex: "gsNum",
                        key: "gsNum",
                        align: "center",
                    },
                    {
                        title: "单位(kg/件)",
                        dataIndex: "gsSpec",
                        key: "gsSpec",
                        align: "center",
                    },
                    {
                        title: "零件(kg)",
                        dataIndex: "gsPart",
                        key: "gsPart",
                        align: "center",
                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "gsInWeight",
                        key: "gsInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "gsBalanceWeight",
                        key: "gsBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "记录重量",
                        dataIndex: "recordWeight",
                        key: "recordWeight",
                        align: "center",
                    },
                    {
                        title: "结存记录重量",
                        dataIndex: "storageRecordWeight",
                        key: "storageRecordWeight",
                        align: "center",
                    },
                    {
                        title: "出库日期",
                        dataIndex: "gsOutTime",
                        key: "gsOutTime",
                        align: "center",
                    },
                    {
                        title: "出库重量(混合领)(kg)",
                        dataIndex: "gsOutMixWeight",
                        key: "gsOutMixWeight",
                        align: "center",
                    },
                    {
                        title: "出库重量(其他领)(kg)",
                        dataIndex: "gsOutOtherWeight",
                        key: "gsOutOtherWeight",
                        align: "center",
                    },
                    {
                        title: "过筛后粉位置",
                        dataIndex: "gsPosition",
                        key: "gsPosition",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "gsQualityState",
                        key: "gsQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "gsAmount",
                        key: "gsAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "gsReportTime",
                        key: "gsReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "gsValidity",
                        key: "gsValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "gsRecheckTime",
                        key: "gsRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "gsEdit",
                        key: "gsEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "gsDepartment",
                        key: "gsDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "gsRemark",
                        key: "gsRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                record.gsBalanceWeight ? '详情':'编辑'
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.gsBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ]
                this.getTableData()
                break;
            case "总混一库存表":
            this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "tqwName",
                        key: "tqwName",
                        align: "center",
                    },
                    {
                        title: "提取物批号",
                        dataIndex: "tqwBatch",
                        key: "tqwBatch",
                        align: "center",

                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "zhyInWeight",
                        key: "zhyInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "zhyBalanceWeight",
                        key: "zhyBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "记录重量",
                        dataIndex: "recordWeight",
                        key: "recordWeight",
                        align: "center",
                    },
                    {
                        title: "结存记录重量",
                        dataIndex: "storageRecordWeight",
                        key: "storageRecordWeight",
                        align: "center",
                    },
                    {
                        title: "GMP报告数",
                        dataIndex: "zhyGmpNum",
                        key: "zhyGmpNum",
                        align: "center",
                    },
                    {
                        title: "出库日期",
                        dataIndex: "zhyOutTime",
                        key: "zhyOutTime",
                        align: "center",
                    },
                    {
                        title: "出库重量(kg)",
                        dataIndex: "zhyOutWeight",
                        key: "zhyOutWeight",
                        align: "center",
                    },
                    {
                        title: "总混一后位置",
                        dataIndex: "zhyPosition",
                        key: "zhyPosition",
                        align: "center",
                    },
                    {
                        title: "失效期",
                        dataIndex: "zhyExpiration",
                        key: "zhyExpiration",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "zhyQualityState",
                        key: "zhyQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "zhyAmount",
                        key: "zhyAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "zhyReportTime",
                        key: "zhyReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "zhyValidity",
                        key: "zhyValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "zhyRecheckTime",
                        key: "zhyRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "zhyEdit",
                        key: "zhyEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "zhyDepartment",
                        key: "zhyDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "zhyRemark",
                        key: "zhyRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                record.zhyBalanceWeight ? '详情':'编辑'
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.zhyBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ]
                this.getTableData()
                break;
            case "总混二库存表":
                this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "zlName",
                        key: "zlName",
                        align: "center",
                    },
                    {
                        title: "颗粒批号",
                        dataIndex: "zlBatch",
                        key: "zlBatch",
                        align: "center",
                    },
                    {
                        title: "入库日期",
                        dataIndex: "zheInTime",
                        key: "zheInTime",
                        align: "center",
                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "zheInWeight",
                        key: "zheInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "zheBalanceWeight",
                        key: "zheBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "记录重量",
                        dataIndex: "recordWeight",
                        key: "recordWeight",
                        align: "center",
                    },
                    {
                        title: "结存记录重量",
                        dataIndex: "storageRecordWeight",
                        key: "storageRecordWeight",
                        align: "center",
                    },
                    {
                        title: "出库日期",
                        dataIndex: "zheOutTime",
                        key: "zheOutTime",
                        align: "center",
                    },
                    {
                        title: "出库重量(kg)",
                        dataIndex: "zheOutWeight",
                        key: "zheOutWeight",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "zheQualityState",
                        key: "zheQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "zheAmount",
                        key: "zheAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "zheReportTime",
                        key: "zheReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "zheValidity",
                        key: "zheValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "zheRecheckTime",
                        key: "zheRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "zheEdit",
                        key: "zheEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "zheDepartment",
                        key: "zheDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "zheRemark",
                        key: "zheRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                record.zheBalanceWeight ? '详情':'编辑'
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.zheBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ];
                this.getTableData();
                break;
            case "制粒库存表":
                this.tableOption.tableColumns = [
                    {
                        title: "序号",
                        dataIndex: "index",
                        key: "index",
                        align: "center",
                        customRender:(text:string,record:any,index:number)=>index+1
                    },
                    {
                        title: "标准类型",
                        dataIndex: "standard",
                        key: "standard",
                        align: "center",
                    },
                    {
                        title: "品名",
                        dataIndex: "zlName",
                        key: "zlName",
                        align: "center",
                    },
                    {
                        title: "颗粒批号",
                        dataIndex: "zlBatch",
                        key: "zlBatch",
                        align: "center",
                    },
                    {
                        title: "制粒产出日期",
                        dataIndex: "zlProdTime",
                        key: "zlProdTime",
                        align: "center",
                    },
                    {
                        title: "件数",
                        dataIndex: "zlNum",
                        key: "zlNum",
                        align: "center",
                    },
                    {
                        title: "单位(kg/件)",
                        dataIndex: "zlSpec",
                        key: "zlSpec",
                        align: "center",
                    },
                    {
                        title: "零件(kg)",
                        dataIndex: "zlPart",
                        key: "zlPart",
                        align: "center",
                    },
                    {
                        title: "入库重量(kg)",
                        dataIndex: "zlInWeight",
                        key: "zlInWeight",
                        align: "center",
                    },
                    {
                        title: "结存重量",
                        dataIndex: "zlBalanceWeight",
                        key: "zlBalanceWeight",
                        align: "center",
                    },
                    {
                        title: "记录重量",
                        dataIndex: "recordWeight",
                        key: "recordWeight",
                        align: "center",
                    },
                    {
                        title: "结存记录重量",
                        dataIndex: "storageRecordWeight",
                        key: "storageRecordWeight",
                        align: "center",
                    },
                    {
                        title: "u8入账",
                        dataIndex: "zlU8",
                        key: "zlU8",
                        align: "center",
                    },
                    {
                        title: "GMP报告数",
                        dataIndex: "zlGmpNum",
                        key: "zlGmpNum",
                        align: "center",
                    },
                    {
                        title: "质量状态",
                        dataIndex: "zlQualityState",
                        key: "zlQualityState",
                        align: "center",
                    },
                    {
                        title: "取样量",
                        dataIndex: "zlAmount",
                        key: "zlAmount",
                        align: "center",
                    },
                    {
                        title: "报告日期",
                        dataIndex: "zhyReportTime",
                        key: "zhyReportTime",
                        align: "center",
                    },
                    {
                        title: "有效期",
                        dataIndex: "zhyValidity",
                        key: "zhyValidity",
                        align: "center",
                    },
                    {
                        title: "复检日期",
                        dataIndex: "zlRecheckTime",
                        key: "zlRecheckTime",
                        align: "center",
                    },
                    {
                        title: "操作人",
                        dataIndex: "zlEdit",
                        key: "zlEdit",
                        align: "center",
                    },
                    {
                        title: "所属部门",
                        dataIndex: "zlDepartment",
                        key: "zlDepartment",
                        align: "center",
                    },
                    {
                        title: "备注",
                        dataIndex: "zlRemark",
                        key: "zlRemark",
                        align: "center",
                    },
                    {
                        align: "center",
                        dataIndex: "action",
                        key: "action",
                        title: "操作",
                        customRender: (text: any, record: any) => {
                            const editBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.edit(record) },
                                },
                                record.zlBalanceWeight ? '详情':'编辑'
                            );
                            const checkBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: { icon: "edit", type: "primary", size: "small" },
                                    on: { click: () => this.check(record) },
                                },
                                "质量检验"
                            );
                            const redoBtn = h(
                                "a-button",
                                {
                                    style: { marginRight: "5px" },
                                    props: {disabled: !record.zlBalanceWeight, icon: "edit", type: "danger", size: "small" },
                                    on: { click: () => this.redo(record) },
                                },
                                "库存调整"
                            );
                            const btn: any[] = [];
                            btn.push(editBtn);
                            btn.push(checkBtn);
                            btn.push(redoBtn);
                            return h("div", btn);
                        },
                    },
                ];
                this.getTableData();
                break;
            default:
                this.tableOption.tableColumns = []
                this.tableOption.tableData = []
        }
        this.tableOption.tableColumns.forEach(item=>{
            item.width = 150
            if (item.title == '操作')item.fixed = 'right'
        })
        this.tableOption.tableColumns[this.tableOption.tableColumns.length-1].width = 320
    }
    formShow = false
    add(){
        this.formShow = true
    }
    formData: any = {}
    formEditId = ''
    viewDetail = false
    edit(record: any){
        this.formShow = true
        this.formEditId = record.id
        this.formData = record
        if(this.curTab==='饮片库存表'){
            this.viewDetail = true
        }
    }
    editQualityShow = false
    check(record:any){
        this.formEditId = record.id
        this.formData = record
        this.editQualityShow = true
    }
    adjustStockShow = false
    adjustStockClose() {
        this.adjustStockShow = false;
        this.tabChange(this.curTab)
    }
    redo(record: any){
        this.adjustStockShow = true;
        // 可根据record初始化form
        this.formData = record
        this.formData.tabType = this.curTab.split('库存表')[0]
    }
    async updateByTable(record:any){
        this.tableOption.loading = true
        const result = await nextApi.productStorageUpdateProd(record)
        this.$message.success(result.msg)
        await this.getTableData()
        this.tableOption.loading = false
    }
    formClose(){
        this.formShow = false
        this.tabChange(this.curTab)
        this.formEditId = ''
        this.viewDetail = false
    }
    downloadTemplate(){
        this.tableOption.loading = true
        nextApi.downloadHalfRawPorductsImportTemplate({type:this.curTab.split('库存表')[0]},(res: any) => {
            const blob = new Blob([res.data], {
                type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                // type: "application/vnd.ms-excel",
            });
            downLoadFile(blob, this.curTab +"导入模板");
            this.tableOption.loading = false;
        });
    }
    beforeUpload(file: any) {
        let formData = new FormData();
        formData.append("file", file);
        formData.append("type",this.curTab.split('库存表')[0])
        this.uploadTemplate(formData);
        return false;
    }
    async uploadTemplate(formData: any) {
        this.tableOption.loading = true;
        try {
            const res = await nextApi.importHalfRawProducts(formData);
            this.$message.success(res.msg);
            await this.getTableData()
        } finally {
            this.tableOption.loading = false;
        }
    }
    onSearch() {
        this.tableOption.pagination.current = 1;
        this.getTableData();
    }
    qualityCheckClose(){
        this.editQualityShow = false
        this.tabChange(this.curTab)
        this.formEditId = ''
    }
    exportData(){
        try {
            this.tableOption.loading = true;
            nextApi.exportHalfRawData((res)=>{
                const blob = new Blob([res.data], {
                type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            });
            downLoadFile(blob, "半成品库存表");
            });
        } catch (error: any) {
            this.$message.error("导出失败：" + (error.message || "未知错误"));
        } finally {
            this.tableOption.loading = false;
        }
    }
    exportDataWithType(){
        try {
            this.tableOption.loading = true;
            nextApi.exportHalfRawDataWithType({type2:this.curTab.split('库存表')[0]}, (res:any)=>{
                const blob = new Blob([res.data], {
                    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                });
                downLoadFile(blob, this.curTab + "数据.xlsx");
            });
        } catch (error: any) {
            this.$message.error("导出失败：" + (error.message || "未知错误"));
        } finally {
            this.tableOption.loading = false;
        }
    }
}
</script>
<style scoped lang="scss">
.filter-panel{
    margin: 10px 0;
}
</style>
